package com.bjfu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.vo.CourseWebVo;
import com.bjfu.eduservice.entity.EduCourse;
import com.bjfu.eduservice.entity.EduCourseDescription;
import com.bjfu.eduservice.entity.frontvo.CourseFrontVo;
import com.bjfu.eduservice.entity.vo.CourseInfoVo;
import com.bjfu.eduservice.entity.vo.CoursePublishVo;
import com.bjfu.eduservice.entity.vo.CourseQuery;
import com.bjfu.eduservice.mapper.EduCourseMapper;
import com.bjfu.eduservice.service.EduChapterService;
import com.bjfu.eduservice.service.EduCourseDescriptionService;
import com.bjfu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.eduservice.service.EduVideoService;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Guli
 * @since 2020-07-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;


    @Override
    @Transactional
    public String saveCourse(CourseInfoVo courseInfoVo) {
        // 添加课程基本信息
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, course);
        int insert = baseMapper.insert(course);
        if (insert < 0) {
            throw new GuliException(20001,"添加课程简介信息失败...");
        }

        // 添加课程描述
        // 课程基本信息和描述表之间是一对一关系
        String cid = course.getId();
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        boolean save = descriptionService.save(courseDescription);
        if(!save){
            throw new GuliException(20001,"添加课程描述信息失败...");
        }
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo vo = new CourseInfoVo();
        // 查询课程基本信息表
        EduCourse course = baseMapper.selectById(courseId);
        BeanUtils.copyProperties(course,vo);
        // 查询课程描述表，描述表和课程表是一对一关系，相同的主键Id
        EduCourseDescription description = descriptionService.getById(courseId);
        if(description != null){
            vo.setDescription(description.getDescription());
        }
        return vo;
    }

    @Override
    @Transactional
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();

        BeanUtils.copyProperties(courseInfoVo,eduCourse);

        int update = baseMapper.updateById(eduCourse);
        if(update <= 0){
            throw new GuliException(20001,"修改课程基本信息失败");
        }

        if(!StringUtils.isEmpty(courseInfoVo.getDescription())){
            EduCourseDescription description = new EduCourseDescription();
            description.setId(courseInfoVo.getId());
            description.setDescription(courseInfoVo.getDescription());
            boolean success = descriptionService.updateById(description);
            if(!success){
                throw new GuliException(20001,"修改课程描述失败");
            }
        }
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        // 调用mapper
        CoursePublishVo vo = baseMapper.getPublishCourseInfo(courseId);
        return vo;
    }

    @Override
    public void getCourseListByPage(Page page, CourseQuery courseQuery) {
        if(courseQuery == null){
            return;
        }
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("status",subjectId);
        }
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("status",subjectParentId);
        }
        if(!StringUtils.isEmpty(teacherId)){
            wrapper.eq("status",teacherId);
        }
        wrapper.orderByDesc("gmt_modified","gmt_create");
        baseMapper.selectPage(page,wrapper);
    }

    @Override
    @Transactional
    public void deleteCourseById(String courseId) {
        //删除顺序：小节->章节->描述->课程
        videoService.removeByCourseId(courseId);
        chapterService.removeByCourseId(courseId);
        descriptionService.removeById(courseId);
        int count = baseMapper.deleteById(courseId);
        if(count < 1){
            throw new GuliException(20001,"删除课程失败");
        }
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("current",pageCourse.getCurrent());
        map.put("records",pageCourse.getRecords());
        map.put("pages",pageCourse.getPages());
        map.put("size",pageCourse.getSize());
        map.put("total",pageCourse.getTotal());
        map.put("next",pageCourse.hasNext());
        map.put("previous",pageCourse.hasPrevious());
        return map;
    }

    @Override
    public CourseWebVo getCourseFrontInfo(String id) {
        CourseWebVo courseWebVo = baseMapper.selectInfoWebById(id);
        return courseWebVo;
    }
}
